package com.gdth.bank.ui;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.gdth.bank.R;
import com.gdth.bank.activity.MenuListActivity;
import com.gdth.bank.activity.WebViewActivity;
import com.gdth.bank.adapter.ImageBrowesAdapter;
import com.gdth.bank.adapter.MainGridViewAdapter;
import com.gdth.bank.bean.FlashBean;
import com.gdth.bank.utils.MenuUtil;
import com.gdth.bank.utils.SharedPreferencesUtil;
import com.gdth.bank.view.MainView;
import com.gdth.core.annotation.view.ViewInject;
import com.gdth.core.widget.BaseFrameLayout;
import com.gdth.model.ImageItem;
import com.gdth.widget.dialog.BaseToast;

public class MainFragment extends BaseFrameLayout implements MainView {

//    @ViewInject(id = R.id.vp)
//    ViewPager viewPager;
//    @ViewInject(id = R.id.fragment_main_gridview, itemClick = "onItemClick")
//    LineGridView mLineGridView;
	@ViewInject(id=R.id.header_label_title) TextView txt_header_title;
	@ViewInject(id=R.id.header_btn_left) TextView txt_header_left;
    @ViewInject(id=R.id.header_btn_right) TextView txt_header_right;
    @ViewInject(id = R.id.tv_text_time)
    TextView tv_text_time;
    @ViewInject(id = R.id.tv_pro_text)
    TextView tv_pro_text;
    @ViewInject(id = R.id.money_is_show)
    ImageButton moneyIsShow;
    @ViewInject(id = R.id.menuRecycler)
    RecyclerView menuRecycler;
    ImageBrowesAdapter mImageBrowesAdapter;
    private List<ImageItem> mList;
    private MainGridViewAdapter gridViewAdapter;
    private List<FlashBean> flashList;
    private static int currentItem = 0; // 当前图片的索引号
    private ScheduledExecutorService scheduledExecutorService;
   // MainPresenter mMainPresenter;
    
    private ArrayList<String> ImageUrl = new ArrayList<>();

    public MainFragment(Context context) {
        super(context, R.layout.fragment_main);
    }

    @Override
    public void initializeView() {
        // TODO Auto-generated method stub
        txt_header_left.setVisibility(View.GONE);
		txt_header_right.setVisibility(View.GONE);
		txt_header_title.setText("主页");
        mList = MenuUtil.getMenuByName("主页");
        WindowManager wm = (WindowManager) getContext()
                .getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        MainGridViewAdapter mGridViewAdapter = new MainGridViewAdapter(mList,width/4);
        menuRecycler.setLayoutManager(new GridLayoutManager(getContext(),4,GridLayoutManager.VERTICAL,false));
        menuRecycler.setHorizontalFadingEdgeEnabled(false);
        menuRecycler.setHorizontalScrollBarEnabled(false);
        menuRecycler.setHasFixedSize(true);
        menuRecycler.setAdapter(mGridViewAdapter);
        menuRecycler.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                ImageItem mImageItem = mList.get(position);

                if (mImageItem.getType() == 2) {
                    Intent intent = new Intent();
                    intent.setClassName(getContext(), mImageItem.getGnlj());
                    getContext().startActivity(intent);
                } else if (mImageItem.getType() == 1) {
                    String key = mImageItem.getKey();
                    String name = mImageItem.getValue();
                    String gnlj = mImageItem.getGnlj();
                    String[] strs = gnlj.split("\\?");
                    if (gnlj.startsWith("com.gdth.bank.activity.WebViewActivity")) {
                        Intent intent = new Intent(getContext(), WebViewActivity.class);
                        intent.putExtra("path", strs[1].toString());
                        intent.putExtra("name", mImageItem.getValue());
                        getContext().startActivity(intent);
                    } else {
                        Intent intent = new Intent(getContext(), MenuListActivity.class);
                        intent.putExtra("key", mImageItem.getKey());
                        intent.putExtra("name", mImageItem.getValue());

                        getContext().startActivity(intent);
                    }

                }
            }
        });
        boolean isShowMoney = SharedPreferencesUtil.getSharePreferencesByKeyForBoolean(context,"paramMoney","isShowMoney");
        if(isShowMoney){
            moneyIsShow.setImageResource(R.drawable.show_money);
        }else{
            moneyIsShow.setImageResource(R.drawable.hide_money);
        }
        moneyIsShow.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String oldString = tv_pro_text.getText().toString();
                if(oldString.equals("*****.**")){
                    tv_pro_text.setText(moneyStr);
                    moneyIsShow.setImageResource(R.drawable.show_money);
                    SharedPreferencesUtil.saveSharePreferenceBoolean(context,"paramMoney","isShowMoney",true);
                }else{
                    tv_pro_text.setText("*****.**");
                    moneyIsShow.setImageResource(R.drawable.hide_money);
                    SharedPreferencesUtil.saveSharePreferenceBoolean(context,"paramMoney","isShowMoney",false);
                }
            }
        });
    }
    String moneyStr = "0.00";

//    boolean isShowMoney = false;
    public  void changeAllMoney(String money, String timeStr,boolean isShowMoney){
        moneyStr = money;
        if(isShowMoney){
            tv_pro_text.setText(money);
        }else{
            tv_pro_text.setText("*****.**");
        }
        tv_text_time.setText(timeStr);
    }

    /**
     * gridview点击事件
     *
     * @param arg0
     * @param arg1
     * @param arg2
     * @param arg3
     */
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
        ImageItem mImageItem = mList.get(arg2);

        if (mImageItem.getType() == 2) {
            Intent intent = new Intent();
            intent.setClassName(this.context, mImageItem.getGnlj());
            this.context.startActivity(intent);
        } else if (mImageItem.getType() == 1) {
            String key = mImageItem.getKey();
            String name = mImageItem.getValue();
            String gnlj = mImageItem.getGnlj();
            String[] strs = gnlj.split("\\?");
            if (gnlj.startsWith("com.gdth.bank.activity.WebViewActivity")) {
                Intent intent = new Intent(this.context, WebViewActivity.class);
                intent.putExtra("path", strs[1].toString());
                intent.putExtra("name", mImageItem.getValue());
                this.context.startActivity(intent);
            } else {
                Intent intent = new Intent(this.context, MenuListActivity.class);
                intent.putExtra("key", mImageItem.getKey());
                intent.putExtra("name", mImageItem.getValue());

                this.context.startActivity(intent);
            }

        }
    }

    @Override
    public void updateFlash(List<FlashBean> arg0) {
        // TODO Auto-generated method stub
        // 初始化图片资源
    }

    @Override
    public void updateView(int type, Object arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void showProgressDialog() {
        // TODO Auto-generated method stub

    }

    @Override
    public void hideProgressDialog() {
        // TODO Auto-generated method stub

    }

    @Override
    public void showMessage(String msg) {
        // TODO Auto-generated method stub
        BaseToast.getInstance().ShowSystemToast(context, msg);
    }


}
